﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MainQuoteDBModel;
using CMSModel;

public partial class Topics_ConcernedPlate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
        }
    }

    private void bd()
    {
        MainQuoteDBEntities dal = new MainQuoteDBEntities();
        ddlBkAdd.DataSource= dal.TB_BK.Where(x => x.BK_CODE.StartsWith("002"));
        ddlBkAdd.DataTextField = "BK_NAME";
        ddlBkAdd.DataValueField = "BK_CODE";
        ddlBkAdd.DataBind();
        ddlGNBk.DataSource=dal.TB_BK.Where(x=>x.BK_CODE.StartsWith("001"));
        ddlGNBk.DataTextField = "BK_NAME";
        ddlGNBk.DataValueField = "BK_CODE";
        ddlGNBk.DataBind();
    }

    protected void btnSaveToDB_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(hidBkId.Value))
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('没有选择板块')", true);
            return;
        }
        //先把新闻保存进数据库 
        News news = new News();
        news.title = txtTitle.Text;
        news.keyword = txtKeyword.Text;
        news.IsEnable = true;
        news.ptime = DateTime.Now;
        news.Summary = txtSummary.Text;
        news.content = txtContent.Text;
        CMSEntities cms = new CMSEntities();
        cms.News.AddObject(news);
        cms.SaveChanges();

        CMSModel.Topics topics = new CMSModel.Topics();
        List<Gzbk> data = new List<Gzbk>();
        Gzbk gzbk = new Gzbk();
        data.Add(gzbk);
        gzbk.BKID = hidBkId.Value;
        gzbk.BKName = lblBkName.Text;
        gzbk.News = new Gzbk_News();
        gzbk.News.Id = news.id;
        gzbk.News.Title = txtTitle.Text;
        gzbk.News.Content = txtSummary.Text;
        gzbk.Stocks = new List<Gzbk_Stock>();
       
        foreach (var c in txtCode.Text.Split(',', '，'))
        {
            Gzbk_Stock gs = new Gzbk_Stock();
            gs.Code = c;
            try
            {
                gs.Name = CommonBll.DicStocks[c];
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('" + c + "股票错误！')", true);
                return;
            }
            gzbk.Stocks.Add(gs);
        }

        topics.Content = Newtonsoft.Json.JsonConvert.SerializeObject(data);
        topics.CTime = DateTime.Now;
        topics.ID = Guid.NewGuid();
        topics.TypeId = Request["typeid"];
        cms.Topics.AddObject(topics);
        cms.SaveChanges();
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功');document.location='Default.aspx'", true);
    }

    protected void btnGaiNian_Click(object sender, EventArgs e)
    {
        hidBkId.Value = ddlGNBk.SelectedValue;
        lblBkName.Text = ddlGNBk.SelectedItem.Text;
        var bkid = ddlGNBk.SelectedValue;
        MainQuoteDBEntities dal = new MainQuoteDBEntities();
        var stocks = dal.TB_BK_GG.Where(x => x.BK_CODE == bkid).Select(x => x.GG_CODE).ToList();
    }

    protected void btnBkAdd_Click(object sender, EventArgs e)
    {
        hidBkId.Value = ddlBkAdd.SelectedValue;
        lblBkName.Text = ddlBkAdd.SelectedItem.Text;
        var bkid = ddlBkAdd.SelectedValue;
        MainQuoteDBEntities dal = new MainQuoteDBEntities();
    }

}